from sqlalchemy import Column, ForeignKey, Integer, String, Text, TIMESTAMP, BOOLEAN,DateTime,Time
from sqlalchemy.ext.declarative import declarative_base
from app.extensions import db
from sqlalchemy.orm import relationship

Base = db.Model


class Question(Base):
    __tablename__ = 'questions'
    question_id = Column(Integer, primary_key=True, autoincrement=True)
    question_type = Column(String(50), nullable=False)
    question_text = Column(Text, nullable=False)
    difficulty_level = Column(Integer)
    subject = Column(String(100))
    tag = Column(String(255))
    description = Column(Text)
    choices = relationship("Choice", back_populates="question")

class Knowledge(Base):
    __tablename__ = 'knowledge'
    id = Column(String(100), primary_key=True)
    parentid = Column(String(100), nullable=False)
    name = Column(String(255), nullable=False)

class Answer(Base):
    __tablename__ = 'answers'

    answer_id = Column(Integer, primary_key=True, autoincrement=True)
    question_id = Column(Integer, ForeignKey('questions.question_id'), nullable=True)
    choice = Column(Integer, nullable=True)
    exercise_id = Column(Integer, ForeignKey('exercise.exercise_id'), nullable=True, comment='对应的练习')

    # Relationships
    question = relationship("Question", backref="answers")
    exercise = relationship("Exercise", backref="answers")


class Choice(Base):
    __tablename__ = 'choices'
    choice_id = Column(Integer, primary_key=True, autoincrement=True)
    question_id = Column(Integer, ForeignKey('questions.question_id'))
    choice_text = Column(Text, nullable=False)
    is_correct = Column(BOOLEAN, nullable=False)
    question = relationship("Question", back_populates="choices")


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    openid = Column(String(255))
    name = Column(String(30))
    age = Column(Integer)


class LessonPlan(Base):
    __tablename__ = 'lessonPlans'
    lp_id = Column(Integer, primary_key=True, autoincrement=True)
    main_knowledge = Column(Text)
    min_age = Column(Integer, nullable=False)
    max_age = Column(Integer, nullable=False)
    class_hour_count = Column(Integer, nullable=False)
    other_requirements = Column(Text)
    teachingContent_analysis = Column(Text)
    students_analysis = Column(Text)
    teaching_objectives = Column(Text)
    teaching_keypoints = Column(Text)
    teaching_difficultyPoints = Column(Text)
    teaching_methods = Column(Text)
    teaching_resource = Column(Text)
    teaching_process = Column(Text)
    teaching_evaluation = Column(Text)
    created_at = Column(TIMESTAMP)
    updated_at = Column(TIMESTAMP)
    user_id = Column(Integer, ForeignKey('user.id'))
    # user = relationship("User", back_populates="lessonPlans")


class Bank(Base):
    __tablename__ = 'bank'

    bank_id = Column(Integer, primary_key=True, autoincrement=True)
    bank_user = Column(Integer, ForeignKey('user.id'), nullable=True, comment='生成题目集的用户')
    bank_name = Column(String(50, collation='utf8mb4_0900_ai_ci'), nullable=True, comment='题目集名字')
    bank_hour = Column(Integer, nullable=True, comment='做题限制小时数')
    bank_minute = Column(Integer, nullable=True, comment='做题限制分钟数')
    bank_second = Column(Integer, nullable=True, comment='做题限制秒数')

    user = relationship('User')


class QuestionBank(Base):
    __tablename__ = 'question_bank'

    id = Column(Integer, primary_key=True, autoincrement=True)
    bank_id = Column(Integer, ForeignKey('bank.bank_id'), nullable=True, comment='题目集id')
    question_id = Column(Integer, ForeignKey('questions.question_id'), nullable=True, comment='题目id')


class Exercise(Base):
    __tablename__ = 'exercise'
    exercise_id = Column(Integer, primary_key=True, autoincrement=True, comment='一次做题(包含多道题目）为一次exercise')
    exercise_finish_datetime = Column(DateTime, nullable=True, default=None, comment='完成练习的时间点')
    exercise_cost_time = Column(Time, nullable=True, default=None, comment='练习花费的时间')
    exercise_bank = Column(Integer, nullable=True, default=None, comment='本次练习对应的题目集')
